package controller

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	"sxk.games/m/v2/core/utils"
)

func Root(c *gin.Context) {
	c.String(http.StatusOK, "hello world !!!")
}

func Json(c *gin.Context) {
	data := map[string]string{
		"name":  "xiaoming",
		"email": "xxxx@qq.com",
	}
	c.JSON(http.StatusOK, data)
}

func Redirect(c *gin.Context) {
	c.Redirect(http.StatusFound, "/json")
}

type DtoApiLogin struct {
	UserName string `json:"userName" form:"userName"`
	Password string `json:"password" form:"password"`
}

func Login(c *gin.Context) {

	reqData := DtoApiLogin{}

	if err := c.Bind(&reqData); err != nil {
		c.Error(err)
		return
	}

	fmt.Println("接收到的数据:", utils.ToJson(reqData))

	if reqData.UserName == "admin" && reqData.Password == "123456" {
		c.String(http.StatusOK, "登陆成功")
	} else {
		c.String(http.StatusOK, "登陆失败")
	}
}
